package data_reader;

import java.util.ArrayList;
import search_alg.AStarAlg;
import search_alg.RouteEdgeAvailability;
import search_alg.RouteNodeInformation;
import search_alg.YenAlg;

import city_model.City;
import city_model.Station;

public class MainClass {
	
	public static void main(String[] args)
	{
     	CityReader cr = new CityReader();
     	City c = cr.readCity("linestations.csv");
//     	for (String s: c.getJunctions().get(0).getStations())
//     	{
//     		System.out.println(c.getJunctions().get(0).getName());
//     		System.out.println(s);
//     	}
     	
//     	TimeReader tr = new TimeReader();
//     	ArrayList<Calendar> times = tr.getTimes("1558", "3683");
//     	for (Calendar cal: times)
//     	{
//     		System.out.println(cal.getTime().toString());
//     	}
     	
//     	System.out.println(c.getStationById("3683").distanceTo(c.getStationById("3684")));
//     	for (Station s:c.getStationsInWalkingDistance(c.getStationById("3683"),maximumWalkingDistance))
//     	{
//     		System.out.println(s.getName());
//     	}
     	
//     	Station s1 = c.getStationById("3683");
//     	Station s2 = c.getStationById("3566");
//     	
     	Station s3 = new Station("0", "start", "start");
     	s3.setCoordinates(Double.parseDouble("45.750045"), Double.parseDouble("21.242054"));
     	Station s4 = new Station("1", "dest", "dest");
     	s4.setCoordinates(Double.parseDouble("45.734503"), Double.parseDouble("21.26702"));
     	
//     	
//     	Station s5 = new Station("2", "dest", "dest");     	
//     	s5.setCoordinates(Double.parseDouble("45.723993"), Double.parseDouble("21.210949"));

     	
		//ArrayList<RouteNodeInformation> path = new AStarAlg().AStarSearch(s3, s4, c, new ArrayList<RouteEdgeAvailability>());
		//displayAStarSearchResut(path, c);
     	long time = System.currentTimeMillis();	
		ArrayList<ArrayList<RouteNodeInformation>> paths = new YenAlg().YenSearch(s3, s4, c, 3);
		long duration = System.currentTimeMillis() - time;
		System.out.println("search ready, duratiom: "+ duration/1000+" seconds");
		//displayYenSearchResut(paths, c);
		
	}
	
	private static void displayAStarSearchResut(ArrayList<RouteNodeInformation> path, City c)
	{
		for (RouteNodeInformation s : path)
		{
			if (s.getNodeId().equals("0"))
			{
				System.out.println("Start station");
			}
			else if (s.getNodeId().equals("1"))
			{
				System.out.println("Station: "+"destination"+" Line: "+"walking"+" Duration: "+s.getTime());
				//System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
			}
			else
			{
				if (s.getTransportMode().equals("w"))
				{
					System.out.println("Station: "+c.getStationById(s.getNodeId()).getFullName()+" Line: "+"walking"+" Duration: "+s.getTime());
					//System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
				}
				else
				{
					System.out.println("Station: "+c.getStationById(s.getNodeId()).getFullName()+" Line: "+ c.getLineById(s.getTransportMode()).getName()+" Duration: "+s.getTime());
				    //System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
				}
			}
		}
		
	}
	
	private static void displayYenSearchResut(ArrayList<ArrayList<RouteNodeInformation>> paths, City c)
	{
		for (ArrayList<RouteNodeInformation> path : paths)
		{
			System.out.println("----- path duration: "+getRouteDuration(path)+" -----");
			for (RouteNodeInformation s : path)
			{
				if (s.getNodeId().equals("0"))
				{
					System.out.println("Start station");
				}
				else if (s.getNodeId().equals("1"))
				{
					//System.out.println("Station: "+"destination"+" Line: "+"walking"+" Duration: "+s.getTime());
					System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
				}
				else
				{
					if (s.getTransportMode().equals("w"))
					{
						//System.out.println("Station: "+c.getStationById(s.getNodeId()).getFullName()+" Line: "+"walking"+" Duration: "+s.getTime());
						System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
					}
					else
					{
						//System.out.println("Station: "+c.getStationById(s.getNodeId()).getFullName()+" Line: "+ c.getLineById(s.getTransportMode()).getName()+" Duration: "+s.getTime());
					    System.out.println("Station: "+s.getNodeId()+" Line: "+ s.getTransportMode()+" Duration: "+s.getTime());
					}
				}
			}
			System.out.println();
		}
		
	}
	
	private static int getRouteDuration(ArrayList<RouteNodeInformation> path)
	{
		int duration = 0;
		for(RouteNodeInformation node: path)
		{
			duration += node.getTime();
		}
		return duration;
	}
}
